﻿using System;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;
using Model.Config;
using Model.Entities_EF;
using Service;

namespace JHClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建HostBuilder 对象用来注入
            var host = new HostBuilder()
                //添加配置文件
                .ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    configApp.AddJsonFile("appsettings.json", optional: true);//添加配置文件
                })
                //注入类
                .ConfigureServices((hostContext, services) =>
                {

                    var mqttSettings = new MqttSettingsModel();
                    hostContext.Configuration.Bind("MQTT", mqttSettings);
                    services.AddSingleton(mqttSettings);


                    services.AddSingleton<Mqtt>();
                    services.AddSingleton<ICPrkmonitorContext>();
                    
                }).ConfigureLogging((hostContext, logging) =>
                {
                    var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

                    // IMPORTANT: This needs to be added *before* configuration is loaded, this lets
                    // the defaults be overridden by the configuration.
                    if (isWindows)
                    {
                        // Default the EventLogLoggerProvider to warning or above
                        logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Error);
                    }

                    logging.AddConsole();
                    logging.AddDebug();
                    logging.AddEventSourceLogger();

                    if (isWindows)
                    {
                        // Add the EventLogLoggerProvider on windows machines
                        logging.AddEventLog();
                    }
                    logging.AddFilter("Microsoft", LogLevel.Error);
                    logging.AddFilter("System", LogLevel.Error);
                })
                .UseConsoleLifetime()
                .Build();
            host.Services.GetService<Mqtt>().StartAsync();//启动监听服务

            while (true)
            {

            }
        }
    }
}
